/**
 * File: NodeCoverageStatistic.java
 * Created by: mhaimel
 * Created on: Mar 26, 2010
 * CVS:  $Id: NodeCoverageStatistic.java 1.0 Mar 26, 2010 5:14:46 PM mhaimel Exp $
 */
package uk.ac.ebi.curtain.model.graph.curtain.filter;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicIntegerArray;

import uk.ac.ebi.curtain.model.graph.Node;
import uk.ac.ebi.curtain.model.graph.GraphAccess.TouchEach;
import uk.ac.ebi.curtain.model.graph.curtain.ContigInfo;
import uk.ac.ebi.curtain.model.graph.curtain.ReadWrapper;

/**
 * @author mhaimel
 *
 */
public class NodeCoverageCombinedFilter implements TouchEach<Node<ContigInfo, ReadWrapper>> {

	private final LinkedBlockingQueue<Node<ContigInfo, ReadWrapper>> valid;
	private final LinkedBlockingQueue<Node<ContigInfo, ReadWrapper>> removed;
	private final AtomicIntegerArray categories;
	private final int min;
	private final int max;

	public NodeCoverageCombinedFilter(int min, int max, int[] categories) {
		this.categories = new AtomicIntegerArray(categories);
		this.min = min;
		this.max = max;
		this.valid = new LinkedBlockingQueue<Node<ContigInfo,ReadWrapper>>();
		this.removed = new LinkedBlockingQueue<Node<ContigInfo,ReadWrapper>>();
	}

	@Override
	public void touch(Node<ContigInfo, ReadWrapper> node) {
		if(null == node){
			return;
		}
		double val = 0d;
		int len = categories.length();
		for(int i = 0; i < len; ++i){
			int cat = categories.get(i);
			val += node.getValue().calculateSequenceCoverage(cat);
		}
		if(val < min){
			this.removed.add(node);
		} else if(min >= max){
			// ignore
		} else if (val > max){
			this.removed.add(node);
		} else {
			this.valid.add(node);
		}
	}

	public LinkedBlockingQueue<Node<ContigInfo, ReadWrapper>> getValid() {
		return valid;
	}
	
	public LinkedBlockingQueue<Node<ContigInfo, ReadWrapper>> getRemoved() {
		return removed;
	}
	
}
